#!/usr/bin/Rscript

library(ggplot2)
library(stargazer)

# Aggregate models (Appendix, Tables A3-A6)

dfcan = read.csv('canada_aggregate.csv')

m1 = glm(uncivil ~ gender*log(follower_count) + offset(log(count)), data=dfcan, family=quasipoisson)
m1a = glm(uncivil ~ gender*log(follower_count) + offset(log(count)) + visible_minority + party + federal, data=dfcan, family=quasipoisson)
m2 = glm(liwc_swear ~ gender*log(follower_count) + offset(log(count)), data=dfcan, family=quasipoisson)
m2a = glm(liwc_swear ~ gender*log(follower_count) + offset(log(count)) + visible_minority + party + federal, data=dfcan, family=quasipoisson)
m3 = glm(swearjar ~ gender*log(follower_count) + offset(log(count)), data=dfcan, family=quasipoisson)
m3a = glm(swearjar ~ gender*log(follower_count) + offset(log(count)) + visible_minority + party + federal, data=dfcan, family=quasipoisson)

stargazer(m1,m2,m3,star.cutoffs = c(0.05,0.01,0.001))
stargazer(m1a,m2a,m3a,star.cutoffs = c(0.05,0.01,0.001))

# USA
dfusa = read.csv('usa_aggregate.csv')

m1 = glm(uncivil ~ gender*log(follower_count) + offset(log(count)), data=dfusa, family=quasipoisson)
m1a = glm(uncivil ~ gender*log(follower_count) + offset(log(count)) + visible_minority + party + log(seniority), data=dfusa, family=quasipoisson)
m2 = glm(liwc_swear ~ gender*log(follower_count) + offset(log(count)), data=dfusa, family=quasipoisson)
m2a = glm(liwc_swear ~ gender*log(follower_count) + offset(log(count)) + visible_minority + party + log(seniority), data=dfusa, family=quasipoisson)
m3 = glm(swearjar ~ gender*log(follower_count) + offset(log(count)), data=dfusa, family=quasipoisson)
m3a = glm(swearjar ~ gender*log(follower_count) + offset(log(count)) + visible_minority + party + log(seniority), data=dfusa, family=quasipoisson)

stargazer(m1,m2,m3,star.cutoffs = c(0.05,0.01,0.001))
stargazer(m1a,m2a,m3a,star.cutoffs = c(0.05,0.01,0.001))

# Figure 2 and A1

dfcan$proportion = dfcan$uncivil/dfcan$count
dfcan = dfcan[dfcan$position =="Prime Minister" | dfcan$position=="Federal Minister" | dfcan$position=="Premier",]
canavg = mean(dfcan$proportion)
dfcan = dfcan[order(-dfcan$proportion),]
df = dfcan[1:20,]
cnames = df[order(-df$proportion),]
cnames = cnames$name
df$ordnames = factor(df$name, levels=cnames)
df$lb = df$proportion-1.96*sqrt(df$proportion*(1-df$proportion)/df$count)
df$ub = df$proportion+1.96*sqrt(df$proportion*(1-df$proportion)/df$count)

pdf("canadianpol.pdf",width=7,height=5,paper='special')
p = ggplot(df,
           aes(x = factor(ordnames, levels=rev(cnames)), color=factor(gender), y = proportion,  
               ymin = lb, ymax = ub)) + 
  geom_linerange(size=.5) + theme_bw() + coord_flip() + 
  geom_point(shape = 15, size=1.5, stroke = .2) +
  expand_limits(y = c(0, 0.3)) +
  scale_y_continuous(breaks = c(0.0,0.1,0.2,0.3)) +
  scale_colour_manual(values=c("darkblue","darkmagenta")) +
  theme(text = element_text(size=12),legend.position="none") +
  geom_hline(yintercept = canavg, linetype = "dashed") + 
  ylab("Proportion of Uncivil Messages") + xlab("") 
p  
dev.off()

dfusa$proportion = dfusa$uncivil/dfusa$count
dfusa = dfusa[dfusa$count>=10000,]
usaavg = mean(dfusa$proportion)
dfusa = dfusa[order(-dfusa$proportion),]
df = dfusa[1:20,]
cnames = df[order(-df$proportion),]
cnames = cnames$name
df$ordnames = factor(df$name, levels=cnames)
df$lb = df$proportion-1.96*sqrt(df$proportion*(1-df$proportion)/df$count)
df$ub = df$proportion+1.96*sqrt(df$proportion*(1-df$proportion)/df$count)

pdf("senators.pdf",width=7,height=5,paper='special')
p = ggplot(df,
           aes(x = factor(ordnames, levels=rev(cnames)), color=factor(gender), y = proportion,  
               ymin = lb, ymax = ub)) + 
  geom_linerange(size=.5) + theme_bw() + coord_flip() + 
  geom_point(shape = 15, size=1.5, stroke = .2) +
  expand_limits(y = c(0, 0.4)) +
  scale_y_continuous(breaks = c(0.0,0.1,0.2,0.3,0.4)) +
  scale_colour_manual(values=c("darkblue","darkmagenta")) +
  theme(text = element_text(size=12),legend.position="none") +
  geom_hline(yintercept = usaavg, linetype = "dashed") + 
  ylab("Proportion of Uncivil Messages") + xlab("") 
p 
dev.off()
